﻿using Newtonsoft.Json;
using ThresholdAssistant.NLogSdk;
using ThresholdAssistant.WebAPI.Models.Constants;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace ThresholdAssistant.WebAPI.Models
{
    /// <summary>
    /// HttpClient类
    /// </summary>
    public class HttpHelper
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="client"></param>
        /// <param name="jsCode"></param>
        /// <returns></returns>
        public async Task<WechatAuthLoginResponse> WechatAuthLoginAsync(HttpClient client, string jsCode)
        {
            var config = AppSettingConstant.WechatAuthLoginGonfig;
            if (config == null)
            {
                return null;
            }
            var url = config.Url;
            var appId = config.AppId; // "wxffa680072f6fe07a";
            var secret = config.Secret; // "7ad92bb0428b6d9149d60db601337017";
            //OAuth
            var request = new HttpRequestMessage()
            {
                Method = HttpMethod.Get,
                RequestUri = new Uri(@$"{url}?appid={appId}&secret={secret}&js_code={jsCode}&grant_type=authorization_code")
            };
            NLogUtil.Debug($"wxlogin:{request.RequestUri}");
            //https://api.weixin.qq.com/sns/jscode2session?appid=wxcc27dddc79d6d141&secret=3a226a2dc840ecfcef5c8f150f4fb551&js_code={wxCode}&grant_type=authorization_code
            var resp = await client.SendAsync(request);
            var message = await resp.Content.ReadAsStringAsync();
            if (string.IsNullOrWhiteSpace(message))
            {
                return null;
            }
            return JsonConvert.DeserializeObject<WechatAuthLoginResponse>(message);
        }

        /// <summary>
        /// Post请求
        /// </summary>
        /// <typeparam name="R"></typeparam>
        /// <typeparam name="T"></typeparam>
        /// <param name="client"></param>
        /// <param name="url"></param>
        /// <param name="data"></param>
        /// <param name="headers"></param>
        /// <param name="contentType"></param>
        /// <returns></returns>
        public async Task<T> PostAsync<R, T>(HttpClient client, string url, R data, Dictionary<string, string> headers = null, string contentType = "application/json")
        {
            //OAuth
            var request = new HttpRequestMessage()
            {
                Method = HttpMethod.Post,
                RequestUri = new Uri(url),
                Content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, contentType)
            };

            if (headers != null)
            {
                foreach (var item in headers)
                {
                    request.Headers.Add(item.Key, item.Value);
                }
            }

            var resp = await client.SendAsync(request);
            var message = await resp.Content.ReadAsStringAsync();
            if (string.IsNullOrWhiteSpace(message))
            {
                return default;
            }
            return JsonConvert.DeserializeObject<T>(message);
        }
    }
}